from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from ktit.soft.models import Catalog
from forms import CatalogForm
from forms import FileprogForm
from models import Fileprog


def catalog_proc(request):
    "A context processor that provides catalog & file list."

    # current catalog
    if "current_catalog" in request.session:
        curcat = int(request.session["current_catalog"])
    else:
        curcat = 1
    cur_catalog	= Catalog.objects.get(id=curcat)	

    # catalog path
    cat_path = []
    cur_cat_path = curcat
    while cur_cat_path!=None:
        ccp = Catalog.objects.get(id=cur_cat_path)
        cat_path.append(ccp)
        if ccp.parent != None:
            cur_cat_path = ccp.parent.id
        else:    
            cur_cat_path = None
    cat_path.reverse()
     
    # catalog list
    cat = Catalog.objects.filter(parent=curcat)
    res = []
    for ci in cat:
        item = {}
        item["id"]=ci.id
        item["name"]=ci.name
        item["parent"]=ci.parent
        item["child"]=Catalog.objects.filter(parent=ci.id) 
        res.append(item)

#gm = ""
#if "global_message" in request.session:
#    gm = request.session["global_message"]

    catalog_list_halflength = (len(res) + 1) / 2 

    return {
        'catalog_list': res,
        'cur_catalog': cur_catalog,
        'catalog_list_halflength': catalog_list_halflength,
        'catalog_path': cat_path,
        'test' : cur_cat_path,
    }


def file_proc(request):

    # current catalog
    if "current_catalog" in request.session:
        curcat = int(request.session["current_catalog"])
    else:
        curcat = 1

    return {
        'file_list': Fileprog.objects.filter(catalog=curcat),
    }

#@login_required
def index(request):
    request.session["current_catalog"] = '1'  
    return render_to_response('soft/index.html', locals(), context_instance=RequestContext(request, processors=[catalog_proc]))


def addcatalog(request):
    if request.method == 'POST':
        form = CatalogForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('ktit.soft.views.catalog', args=[request.session["current_catalog"]]))
    else:
        form = CatalogForm( initial={'parent': request.session["current_catalog"]} )
    return render_to_response('soft/form_catalog_add.html', locals(), context_instance=RequestContext(request, processors=[catalog_proc]))


def addfile(request):
    if request.method == 'POST':
        form = FileprogForm(request.POST, request.FILES)
        if form.is_valid():

            #instance = Fileprog() 
            #instance.uploaded_by = request.user 
            #save_instance(form, instance) 
            #request.user.message_set.create(message=_('Your file has been received.')) 



            form.save()
            return HttpResponseRedirect(reverse('ktit.soft.views.catalog', args=[request.session["current_catalog"]]))



     

    else:
        #file_data = {'file': {'filename':'face.jpg', 'content': 'file data'}}file_data

        form = FileprogForm( initial={'catalog': request.session["current_catalog"]} )
    return render_to_response('soft/form_file_add.html', locals(), context_instance=RequestContext(request, processors=[catalog_proc]))



def catalog(request, catid):
    request.session["current_catalog"] = catid
    return render_to_response('soft/index.html', locals(), context_instance=RequestContext(request, processors=[catalog_proc, file_proc]))

#def file(request, fid):
